//The MIT License
//
//Copyright (c) 2009 nodchip
//
//Permission is hereby granted, free of charge, to any person obtaining a copy
//of this software and associated documentation files (the "Software"), to deal
//in the Software without restriction, including without limitation the rights
//to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
//copies of the Software, and to permit persons to whom the Software is
//furnished to do so, subject to the following conditions:
//
//The above copyright notice and this permission notice shall be included in
//all copies or substantial portions of the Software.
//
//THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
//IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
//FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
//AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
//LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
//OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
//THE SOFTWARE.
package tv.dyndns.kishibe.client.statistics;

import tv.dyndns.kishibe.client.Service;
import tv.dyndns.kishibe.client.UserData;
import tv.dyndns.kishibe.client.util.IntArrays;

import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.HTML;
import com.google.gwt.user.client.ui.VerticalPanel;
import com.googlecode.gchart.client.GChart;
import com.googlecode.gchart.client.GChart.SymbolType;

public class PanelStatisticsRatingHistory extends VerticalPanel {
	private static final PanelStatisticsRatingHistory instance = new PanelStatisticsRatingHistory();

	public static PanelStatisticsRatingHistory getInstance() {
		return instance;
	}

	private boolean first = true;
	private final AsyncCallback<int[]> callbackGetRatingHistory = new AsyncCallback<int[]>() {
		public void onSuccess(int[] result) {
			display(result);
		}

		public void onFailure(Throwable caught) {
		}
	};

	private PanelStatisticsRatingHistory() {
	}

	public void resetFlag() {
		first = true;
	}

	private void display(final int[] data) {
		IntArrays.reverse(data);

		final int min = IntArrays.minValue(data);
		final int max = IntArrays.maxValue(data);
		final int average = IntArrays.average(data);

		if (min == Integer.MAX_VALUE) {
			add(new HTML("未プレイ"));
			return;
		}

		clear();

		add(new RatingHistoryChart(data, min, max, average));
	}

	protected void onLoad() {
		super.onLoad();

		if (!first) {
			return;
		}
		first = false;

		Service.Util.getInstance().getRatingHistory(UserData.getInstance().getUserCode(), callbackGetRatingHistory);
	}

	public class RatingHistoryChart extends GChart {
		public RatingHistoryChart(int[] data, int min, int max, int average) {
			super(512, 384);
			setChartTitle("<b><big>レーティング履歴</big></b>");
			setChartFootnotesLeftJustified(true);
			setChartFootnotes("過去100戦のレーティングです<br>個人レーティングの計算式は<a href='http://ja.wikipedia.org/wiki/%E3%82%A4%E3%83%AD%E3%83%AC%E3%83%BC%E3%83%86%E3%82%A3%E3%83%B3%E3%82%B0' target='_blank'>こちら</a><br>"//
					+ "最低レーティング…" + min + "<br>"//
					+ "最高レーティング…" + max + "<br>"//
					+ "平均レーティング…" + average);
			addCurve();
			for (int i = 0; i < data.length; ++i) {
				getCurve().getSymbol().setWidth(5);
				getCurve().getSymbol().setHeight(5);
				getCurve().addPoint(i, data[i]);
			}

			getCurve().getSymbol().setSymbolType(SymbolType.LINE);
			getXAxis().setAxisLabel("過去の戦歴");
			getXAxis().setTickLabelFormat("#");
			getYAxis().setAxisLabel("レーティング");
			getYAxis().setTickLabelFormat("#");
			getYAxis().setHasGridlines(true);

			update();
		}
	}
}
